package com.jic.point.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.RestResult;
import com.jic.point.constant.consist.ResponseCode;
import com.jic.point.exception.AppRuntimeException;
import com.jic.point.model.entity.ConfBd;
import com.jic.point.model.entity.ConfBdApply;
import com.jic.point.model.vo.request.ConfBdRequest;
import com.jic.point.model.vo.response.ConfBdResponse;
import com.jic.point.service.ConfBdApplyService;
import com.jic.point.service.ConfBdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
/**
 * @Author: tzc
 * @date: 2020-10-11 17:28:32
 */
 @RestController
 @Slf4j
 @RequestMapping("/confBd")
 @Api(tags = " [BD部门表]控制类-PC")
public class ConfBdController {
	@Autowired
	private ConfBdService service;

	@Resource
    ConfBdApplyService confBdApplyService;
	
     /**
      *新增记录
      * @param request 新增[BD部门表]请求参数
      * @return ConfBdResponse 新增[BD部门表]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
    @ApiOperation(value = "新增[BD部门表]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody ConfBdRequest request) throws Throwable {
     		ConfBd confBd = new ConfBd();
    		BeanUtils.copyProperties(request, confBd);
    	    long flag = service.save(confBd);
     return RestResult.success(flag);
    }
	
     /**
      * 新增或修改记录
      * @param request 新增或修改[BD部门表]请求参数
      * @return ConfBdResponse 新增或修改[BD部门表]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "保存或更新[BD部门表]")
     @PostMapping("/saveOrUpdate")
      @ResponseBody
     public RestResult<Long> saveOrUpdate(@Validated @RequestBody ConfBdRequest request) throws Throwable {
      		 ConfBd confBd = new ConfBd();
		     BeanUtils.copyProperties(request, confBd);
	         long flag =   service.saveOrUpdate(confBd);
	         return RestResult.success(flag);
     }
	
     /**
      * 删除记录
      * @param request 删除[BD部门表]请求参数
      * @return ConfBdResponse 删除[BD部门表]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "删除[BD部门表]记录")
     @PostMapping("/delete")
     @ResponseBody
     public RestResult<Long> delete(@RequestBody ConfBdRequest request) throws Throwable {
      		ConfBd confBd = new ConfBd();
	     	BeanUtils.copyProperties(request, confBd);
	     	ConfBd result = service.getByPrimaryKey(confBd);
	     	//主表逻辑删除
            service.deleteByPrimaryKeyLogically(confBd);
            //操作人
             result.setApplierId(request.getAdminOperate().getOperator());
             //设置当前动作
             result.setCurrentAction(2);
             //添加到申请表中
             ConfBdApply confBdApply = new ConfBdApply();
             BeanUtils.copyProperties(result, confBdApply);
             long flag = confBdApplyService.insertSelective(confBdApply);
             return RestResult.success(flag);
     }
	
     /**
      * 修改记录
      * @param request 修改[BD部门表]请求参数
      * @return ConfBdResponse 修改[BD部门表]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "修改[BD部门表]记录")
     @PostMapping("/update")
     @ResponseBody
     public RestResult<Long> update(@RequestBody ConfBdRequest request) throws Throwable {
      		ConfBd confBd = new ConfBd();
	     	BeanUtils.copyProperties(request, confBd);
	         long flag = service.updateByPrimaryKeySelective(confBd);
      return RestResult.success(flag);
     }
	
     /**
      * 单表查询详情信息
      * @param request 查询[BD部门表]详情请求参数
      * @return ConfBdResponse 查询[BD部门表]详情响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
      @PostMapping("/getByPrimaryKey")
      @ApiOperation(value = "查询[BD部门表]详情")
      @ResponseBody
     public RestResult<ConfBdResponse> getByPrimaryKey(@Validated  @RequestBody ConfBdRequest request) {
     	log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
	     if (request!=null && request.getId()!=null) {
	      
	     	ConfBd confBd = new ConfBd();
	     	BeanUtils.copyProperties(request, confBd);
	     	ConfBd result = service.getByPrimaryKey(confBd);
		     log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
		     if (result == null) {
		     	return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
		     }
		     ConfBdResponse bo = new ConfBdResponse();
		     BeanUtils.copyProperties(result, bo);
	     	return RestResult.success(bo);
	     }
     	log.info("查询详情请求结束");
	     return RestResult.success(new ConfBdResponse());
     }
	
    /**
      * 分页查询
      *
      * @param request 分页查询请求参数
      * @return 分页列表
      * @throws Throwable 业务异常和系统异常
      */
     @PostMapping("/listPage")
     @ApiOperation(value = " 分页查询列表 ")
     @ResponseBody
     public RestResult<PageInfo<ConfBd>> listPage (@RequestBody ConfBdRequest request) throws Throwable {
      ConfBd param = new ConfBd();
      BeanUtils.copyProperties(request, param);
      param.setType(0);
      param.setDeleteFlag(0);
     return RestResult.success(service.listPageBySelective(param, request.getPageNum(),request.getPageSize()));
     }
     
	
      /**
       *CONF_BD 查询列表（不分页）
       * @param  request 前端请求参数
       */
      @PostMapping("/listAll")
      @ApiOperation(value = "不分页查询 [BD部门表] ")
      @ResponseBody
      public RestResult<List<ConfBdResponse>>  listConfBdAllRecord(@RequestBody ConfBdRequest request) throws Throwable {
      		ConfBd param = new ConfBd();
	     	BeanUtils.copyProperties(request, param);
	     	param.setDeleteFlag(0);
	     	List<ConfBd> list = service.listAllRecord(param);
			List<ConfBdResponse> responseList = list.stream().map(temp -> {
			ConfBdResponse bo = new ConfBdResponse();
			BeanUtils.copyProperties(temp, bo);
			return bo;
			}).filter(item -> item != null).collect(Collectors.toList());
		    return RestResult.success(responseList);
      }

    /**
     * 查询部门名称和部门id用做于下拉框
     */
    @PostMapping("/query")
    @ResponseBody
    public RestResult<List<ConfBdResponse>>  query() throws Throwable {
          List<ConfBdResponse> list= service.query();
        return RestResult.success(list);
    }

    /**
     * 冻结与申请
     * @param request
     * @return
     */
    @PostMapping("/freezeApplication")
    @ResponseBody
    public RestResult<Long> freezeApplication(@Validated @RequestBody ConfBdRequest request){
        log.info("查询冻结与解冻参数入参，{}", JSONObject.toJSONString(request));
        //根据id查询到信息
        ConfBd confBd = new ConfBd();
        BeanUtils.copyProperties(request, confBd);
        ConfBd result = service.getByPrimaryKey(confBd);
        //主表逻辑删除
        service.deleteByPrimaryKeyLogically(confBd);
        //将主表申请的信息添加到申请表中
        result.setSts(request.getSts());
        result.setApplyReason(request.getApplyReason());
        result.setApplierId(request.getAdminOperate().getOperator());

        //插入到申请表
        ConfBdApply confBdApply = new ConfBdApply();
        BeanUtils.copyProperties(result, confBdApply);
        confBdApply.setCurrentAction(1);
        confBdApply.setAuditSts(0);
        confBdApply.setDeleteFlag(0);
        long flag = confBdApplyService.insertSelective(confBdApply);
        return RestResult.success(flag);
    }

    /**
     * 延期申请
     * @param request
     * @return
     */
    @PostMapping("/delayApply")
    @ResponseBody
    public RestResult<Long> delayApply(@Validated @RequestBody ConfBdRequest request){
        log.info("查询冻结与解冻参数入参，{}", JSONObject.toJSONString(request));
        //根据id查询到信息
        ConfBd confBd = new ConfBd();
        BeanUtils.copyProperties(request, confBd);
        ConfBd result = service.getByPrimaryKey(confBd);
        if(result.getEndTime().getTime() >= request.getEndTime().getTime()){
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "延期时间需大于活动结束时间");
        }
        //主表逻辑删除
        service.deleteByPrimaryKeyLogically(confBd);
        //赋值
        result.setEndTime(request.getEndTime());
        result.setApplyReason(request.getApplyReason());
        result.setApplierId(request.getAdminOperate().getOperator());
        //插入到申请表
        ConfBdApply confBdApply = new ConfBdApply();
        BeanUtils.copyProperties(result, confBdApply);
        confBdApply.setCurrentAction(1);
        confBdApply.setDeleteFlag(0);
        confBdApply.setAuditSts(0);
        long flag = confBdApplyService.insertSelective(confBdApply);
        return RestResult.success(flag);
    }

    /**
     * 提前申请
     * @param request
     * @return
     */
    @PostMapping("/advanceApply")
    @ResponseBody
    public RestResult<Long> advanceApply(@Validated @RequestBody ConfBdRequest request){
        log.info("查询冻结与解冻参数入参，{}", JSONObject.toJSONString(request));
        //根据id查询到信息
        ConfBd confBd = new ConfBd();
        BeanUtils.copyProperties(request, confBd);
        ConfBd result = service.getByPrimaryKey(confBd);
        if(result.getEndTime().getTime() <= request.getEndTime().getTime()){
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "提前时间需小于活动结束时间");
        }
        //主表逻辑删除
        service.deleteByPrimaryKeyLogically(confBd);
        //赋值
        result.setEndTime(request.getEndTime());
        result.setApplyReason(request.getApplyReason());
        result.setApplierId(request.getAdminOperate().getOperator());
        //插入到申请表
        ConfBdApply confBdApply = new ConfBdApply();
        BeanUtils.copyProperties(result, confBdApply);
        confBdApply.setCurrentAction(1);
        confBdApply.setDeleteFlag(0);
        confBdApply.setAuditSts(0);
        long flag = confBdApplyService.insertSelective(confBdApply);
        return RestResult.success(flag);
    }

    /**
     * 树形结构
     * @return
     */
    @PostMapping("/Tree")
    @ResponseBody
    public RestResult<List<ConfBdResponse>> Terr(){
        List<ConfBdResponse> responses = service.getTree();
        if (responses==null){
            return RestResult.error("8888","查询失败");
        }
        return RestResult.success(responses);
    }



}

	
